import java.util.*;


public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;

    /**
     * 创建一棵二叉树 创建成功后 返回根节点
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    void preOrderNor(TreeNode root){
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    /*List<Integer> ret = new ArrayList<>();
    *//**
     * 把 前序遍历的结果 存储到List 当中
     * @param root
     * @return
     *//*
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null) {
            return null;
        }
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ret;
    }*/
    /*public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        list.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        list.addAll(rightTree);
        return list;
    }*/
    // 中序遍历  -》 左根右
    void inOrder(TreeNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    void inOrderNor(TreeNode root){
       if (root == null) {
           return;
       }

       Stack<TreeNode> stack = new Stack<>();
       TreeNode cur = root;
       while( cur != null ||!stack.isEmpty()) {
           while (cur != null) {
               stack.push(cur);
               cur = cur.left;
           }

           TreeNode top = stack.pop();
           System.out.print(top.val + " ");
           cur = top.right;
       }
    }




    // 后序遍历  -》 左右根
    void postOrder(TreeNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }




    public int nodeSize;
    // 获取树中节点的个数
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }

    //子问题的思路解决
    int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }

    public int leafSize;
    // 获取叶子节点的个数
    int getLeafNodeCount1(TreeNode root){
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return leafSize;
    }

    // 子问题解决
    int getLeafNodeCount(TreeNode root){
       if(root == null) {
           return 0;
       }

       if (root.left == null && root.right == null) {
           return 1;
       }
       return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }


    int  getKLevelNodeCount(TreeNode root,int k) {
       if (root == null) {
           return 0;
       }
       if (k == 1) {
           return 1;
       }

       return getKLevelNodeCount(root.left, k-1) + getKLevelNodeCount(root.right, k-1);
    }

    // 或缺二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH + 1:  rightH + 1;
    }
    // 检测值为value的元素是否存在
    private TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }

        if (root.val == val) {
            return root;
        }
        TreeNode leftV = find(root.left, val);
        if (leftV != null) {
            return leftV;
        }
        TreeNode rightV = find(root.right, val);
        if (rightV != null) {
            return rightV;
        }
        return null;

    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        // p 等于空 或者 去等于空
        if((p == null && q != null) || (p != null && q == null)) {
            return false;
        }

        // p 和 q 都等于空
        if (p == null &&  q == null) {
            return true;
        }
        // p 和 q 都不等于空
        if (p.val != q.val) {
            return false;
        }

        // p != null && q != null  && p.val == q.val
        return isSameTree(p.left, q.left) &&  isSameTree(p.right, q.right);

    }

    // 另一颗树的子树  https://leetcode.cn/problems/subtree-of-another-tree/
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null || subRoot == null) {
            return false;
        }
        if(isSameTree(root, subRoot)) {
            return true;
        }

        if (isSubtree(root.left, subRoot)) {
            return true;
        }

        if (isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return root;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);

        return root;
    }

    // 对称二叉树 https://leetcode.cn/problems/symmetric-tree/
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }

        return isSymmetricChild(root.left, root.right);

    }

    private boolean  isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
        if (leftTree == null && rightTree == null) {
            return true;
        }

        if ((leftTree == null && rightTree != null) || (leftTree != null && rightTree == null)) {
            return false;
        }

        if (leftTree.val != rightTree.val) {
            return false;
        }

        return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
    }

    //层序遍历
    void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
      if (root == null) {
          return true;
      }

      Queue<TreeNode> queue = new LinkedList<>();
      queue.offer(root);

      while (!queue.isEmpty()) {
          TreeNode cur = queue.poll();
          if (cur != null) {
              queue.offer(cur.left);
              queue.offer(cur.right);
          } else {
              break;//结束这个循环
          }
      }

      // 判断队列里是否有非空元素
      while (!queue.isEmpty()) {
          TreeNode nullCur = queue.peek();
          // 每个元素都出队判断是不是空
          if (nullCur == null) {
              queue.poll();
          } else {
              return  false;
          }
      }
      return true;
    }



}
